package in.muton.edushare.auth.api;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.auth.roles.DeptRoleCheck;
import in.muton.edushare.auth.roles.UnivRoleCheck;
import in.muton.edushare.processing.objects.UserDataHandler;
import in.muton.edushare.wicket.models.CayenneModel;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class Authentication implements Serializable{

	private static final Log log = LogFactory.getLog(Authentication.class);

	public Authentication(Universities univ, Users loggedInUser) {
		this.user = loggedInUser;
		isOwnerUniv = true;
		parser = new SettingsParser(univ.getSettings());
		if (UnivRoleCheck.isAdmin(univ, user)) {
			isPrimaryAdmin = true;
			isUnivMember = true;
		} else if (UnivRoleCheck.isTeacher(univ, user)) {
			isUnivMember = true;
			isTeacher = true;
		} else if (UnivRoleCheck.isStudent(univ, user)) {
			isStudent = true;
			isUnivMember = true;
		} else {
		}
	}

	public Authentication(Departments dept, Users user) {
		this.user = user;
		parser = new SettingsParser(dept.getSettings());
		isOwnerDept = true;
		if (DeptRoleCheck.isPrimaryAdmin(dept, user)
				|| DeptRoleCheck.isUnivAdmin(dept, user)) {
			isPrimaryAdmin = true;
			isDeptMember = true;
			isUnivMember = true;
		} else if (DeptRoleCheck.isSecondaryAdmin(dept, user)
				|| DeptRoleCheck.isTeacher(dept, user)) {
			isTeacher = true;
			isDeptMember = true;
			isUnivMember = true;
		} else if (DeptRoleCheck.isStudent(dept, user)) {
			isStudent = true;
			isDeptMember = true;
			isUnivMember = true;
		} else if (DeptRoleCheck.isUnivMember(dept, user)) {
			isUnivMember = true;
		} else {
		}
	}

	public Authentication(CourseInstances course, Users user) {
		this.user = user;
		parser = new SettingsParser(course.getSettings());
		isOwnerCourse = true;
		CourseRoleCheck crc = new CourseRoleCheck(course, user);
		if (crc.isAdmin()) {
			isPrimaryAdmin = true;
			isDeptMember = true;
			isUnivMember = true;
		} else if (crc.isTeacher()) {
			isTeacher = true;
			isDeptMember = true;
			isUnivMember = true;
		} else if (crc.isStudent()) {
			isStudent = true;
			isDeptMember = true;
			isUnivMember = true;
		} else if (CourseRoleCheck.isDeptMember(course, user)) {
			isDeptMember = true;
			isUnivMember = true;
		} else if (CourseRoleCheck.isUnivMember(course, user)) {
			isUnivMember = true;
		} else {
		}
	}

	public Authentication(Users pageOwner, Users loginUser) {
		UserDataHandler udh = new UserDataHandler(new CayenneModel<Users>(pageOwner));
		parser = new SettingsParser(pageOwner.getSettings());
		isOwnerUser = true;
		List<IModel<Universities>> uList = udh.getUniv().getObject();
		for (IModel<Universities> u : uList) {
			UnivRoleCheck urc = new UnivRoleCheck(u.getObject(), loginUser);
			if (urc.isAdmin()) {
				isPrimaryAdmin = true;
				isUnivMember = true;
			} else if (urc.isTeacher()) {
				isTeacher = true;
				isUnivMember = true;
			} else if (urc.isMember()) {
				isUnivMember = true;
			}
		}
		// dept member is true for any member of any dept the user is part of.
		List<IModel<Departments>> dList = udh.getAllDepts().getObject();
		for (IModel<Departments> d : dList) {
			DeptRoleCheck drc = new DeptRoleCheck(d.getObject(), loginUser);
			if (drc.isPrimaryAdmin() || drc.isUnivAdmin()) {
				isPrimaryAdmin = true;
				isDeptMember = true;
			} else if (drc.isSecondaryAdmin() || drc.isTeacher()) {
				isTeacher = true;
				isDeptMember = true;
			} else if (drc.isMember()) {
				isDeptMember = true;
			}
		}
		// is teacher is true for any teacher of any course the user is
		// attending.
		List<IModel<CourseInstances>> cList = udh.getActiveCourseInst()
				.getObject();
		for (IModel<CourseInstances> c : cList) {
			CourseRoleCheck crc = new CourseRoleCheck(c.getObject(), loginUser);
			if (crc.isAdmin()) {
				isPrimaryAdmin = true;
			} else if (crc.isTeacher()) {
				isTeacher = true;
			} else if (crc.isStudent()) {
				isStudent = true;
			}
		}
	}

	public Authentication(Groups group, Users loggedInUser) {
		parser = new SettingsParser(group.getSettings());
		if(loggedInUser.isGroupAdmin(group)){
			isGroupAdmin = true;
		}
		
		if(loggedInUser.isGroupMember(group)){
			isGroupMember = true;
		}
		
	}

	public void changeSetting(String settingName, String newSetting) {
		parser.setSetting(settingName, newSetting);
	}

	public String getSetting(String settingName) {
		return parser.getSetting(settingName);
	}

	public boolean isAllowed(String actionName) {
		log.debug("Action name is " + actionName);
		String rights = parser.getSetting(actionName);
		if (isOwnerUser) {
			if (isPrimaryAdmin || isTeacher)
				return true;
		}
		log.debug("Rights are " + rights);

		log.debug("isPrimaryAdmin is" + isPrimaryAdmin);
		log.debug("isTeacher is" + isTeacher);
		log.debug("isStudent is" + isStudent);
		log.debug("isUnivMember is" + isUnivMember);
		log.debug("isDeptMember is" + isDeptMember);

		if (rights.compareTo(ANYONE) == 0)
			return true;
		if (rights.compareTo(ADMINS) == 0 && isPrimaryAdmin == true)
			return true;
		if (rights.compareTo(TEACHERS) == 0
				&& (isTeacher == true || isPrimaryAdmin == true))
			return true;
		if (rights.compareTo(COURSE_ONLY) == 0 && isStudent == true)
			return true;
		if (rights.compareTo(DEPT_ONLY) == 0 && isDeptMember == true)
			return true;
		if (rights.compareTo(ALL_UNIV) == 0 && isUnivMember == true)
			return true;

		if(rights.compareTo(GROUP_MEMBERS) == 0 && isGroupMember == true)
			return true;
		
		if(rights.compareTo(GROUP_ADMINS) == 0 && isGroupAdmin == true)
			return true;
		
		return false;
	}

	public String getString() {
		return parser.getString();
	}

	private boolean isOwnerUser = false;
	private boolean isOwnerDept = false;
	private boolean isOwnerUniv = false;
	private boolean isOwnerCourse = false;

	private boolean isPrimaryAdmin = false;
	private boolean isTeacher = false;
	private boolean isStudent = false;
	private boolean isUnivMember = false;
	private boolean isDeptMember = false;
	
	private boolean isGroupMember = false;
	private boolean isGroupAdmin = false;
	
	private Users user = null;
	private SettingsParser parser = null;

	public static final String MSG_SND = "zms";
	public static final String MSG_VIEW = "zmv";
	public static final String CAL_VIEW = "zcv";
	public static final String CAL_NEW = "zcc";
	public static final String FILE_UP = "zfu";
	public static final String FILE_DN = "zfd";
	public static final String PROG_NEW = "zpn";
	public static final String COURSE_NEW = "zcn";
	public static final String SEM_NEW = "zss";

	public static final String NOTIFY_EVT = "eNotify";
	public static final String NOTIFY_ASS = "aNotify";
	public static final String NOTIFY_POL = "pNotify";
	public static final String NOTIFY_MSG = "mNotify";
	public static final String NOTIFY_FIL = "fNotify";

	public static final String TRUE = "t";
	public static final String FALSE = "f";

	public static final String ANYONE = "qa";
	public static final String ADMINS = "qb";
	public static final String TEACHERS = "qc";
	public static final String ALL_UNIV = "qd";
	public static final String DEPT_ONLY = "qe";
	public static final String COURSE_ONLY = "qf";
	public static final String DEPT_MAIN_ADMINS = "qg";
	public static final String ADMIN_TEACHERS = "qh";
	public static final String GROUP_ADMINS = "qj";
	public static final String GROUP_MEMBERS = "qk";
}
